package org.wenhua.yuan.and.module_rn

import android.widget.Toast
import com.facebook.react.bridge.*
import com.facebook.react.bridge.UiThreadUtil.runOnUiThread

class JRNBridge (reactContext: ReactApplicationContext)
    : ReactContextBaseJavaModule(reactContext), IRNBridge<ReadableMap, Promise> {
    override fun getName(): String {
        // 这个String会在ReactNative端被这里使用，所以name命名须谨慎
        // import {NativeModules} from 'react-native'
        // module.exports = NativeModules.RNBridge
        return "RNBridge"
    }

    @ReactMethod
    override fun onBack(p: ReadableMap?) {
        if (currentActivity != null) {
            runOnUiThread {
                currentActivity!!.onBackPressed()
            }
        }
    }
    @ReactMethod
    override fun gotoNativeMethod(p: ReadableMap) {
        // 页面跳转方法，p是携带数据的map类型
    }
    @ReactMethod
    override fun getHeaders(callback: Promise) {
        val param: WritableMap = Arguments.createMap();
        param.putString("token", "login_token")
        param.putString("useInfo", "用户信息")
        callback.resolve(param)
    }

    @ReactMethod
    override fun toast(p: ReadableMap) {
        Toast.makeText(currentActivity!!, p.getString("toast"), Toast.LENGTH_SHORT).show()
    }


}